//
//  FDMMasterCollectionViewCell.m
//  Entertainment
//
//  Created by dllo on 16/4/22.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "FDMMasterCollectionViewCell.h"
#import "FDMMasterModel.h"
#import "FDMMasterKindModel.h"
#import "FDMMasterPersonalCollectionViewCell.h"
#import "FDMHotRecommendsHeaderView.h"
#import "FDMMasterDetailViewController.h"

@interface FDMMasterCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) FDMBaseCollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataSourceArr;
@property (nonatomic, retain) NSMutableArray *masterArrayOfNew;

@end
@implementation FDMMasterCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self dataHandel];
        [self createCollectionView];
        self.dataSourceArr = [NSMutableArray array];
        self.masterArrayOfNew = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc {
    [self.collectionView release];
    [super dealloc];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 42) / 3, 180);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[FDMBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 153) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00];
    [self.contentView addSubview:self.collectionView];
    [self.collectionView registerClass:[FDMMasterPersonalCollectionViewCell class] forCellWithReuseIdentifier:@"FDMMasterPersonalCollectionViewCell"];
    [self.collectionView registerClass:[FDMMasterPersonalCollectionViewCell class] forCellWithReuseIdentifier:@"FDMMasterPersonalCollectionViewCellSectionZero"];
    // 新晋主播
    [self.collectionView registerClass:[FDMMasterPersonalCollectionViewCell class] forCellWithReuseIdentifier:@"NewMaster"];
    [self.collectionView registerClass:[FDMHotRecommendsHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDMMasterHeader"];
    [self.collectionView registerClass:[FDMHotRecommendsHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDMMasterHeaderOne"];
    [self.collectionView registerClass:[FDMHotRecommendsHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDMMasterHeaderZero"];
    
    
}

#pragma mark - collectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.dataSourceArr.count + 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        if (self.dataSourceArr.count) {
            FDMMasterKindModel *model = self.dataSourceArr[section];
            return model.listModelArr.count;
        }
    }
    if (section == 1) {
        return self.masterArrayOfNew.count;
    } else {
        if (self.dataSourceArr.count) {
            FDMMasterKindModel *model = self.dataSourceArr[section- 1];
            return model.listModelArr.count;
        }
    }
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, 30);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (kind == UICollectionElementKindSectionHeader) {
            FDMHotRecommendsHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMMasterHeaderZero" forIndexPath:indexPath];
            if (self.dataSourceArr.count) {
                FDMMasterKindModel *model = self.dataSourceArr[indexPath.section];
                header.titleLabel.text = model.title;
                header.backgroundColor = [UIColor whiteColor];
            }
            return header;
        }
    }
    if (indexPath.section == 1) {
        if (kind == UICollectionElementKindSectionHeader) {
            FDMHotRecommendsHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMMasterHeaderOne" forIndexPath:indexPath];
            header.titleLabel.text = @"新晋主播";
            header.backgroundColor = [UIColor whiteColor];
            return header;
        }
    }
    if (indexPath.section > 1) {
        if (kind == UICollectionElementKindSectionHeader) {
            FDMHotRecommendsHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMMasterHeader" forIndexPath:indexPath];
            FDMMasterKindModel *model = self.dataSourceArr[indexPath.section - 1];
            header.titleLabel.text = model.title;
            header.backgroundColor = [UIColor whiteColor];
            return header;
        }
    }
        return nil;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        FDMMasterPersonalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMMasterPersonalCollectionViewCellSectionZero" forIndexPath:indexPath];
        if (self.dataSourceArr.count) {
            FDMMasterKindModel *model = self.dataSourceArr[indexPath.section];
            FDMMasterModel *detailModel = model.listModelArr[indexPath.row];
            if (model.listModelArr.count) {
                 cell.detailModel = detailModel;
            }            
        }
        // 指定代理人
        cell.layer.borderColor = [UIColor grayColor].CGColor;
        cell.layer.borderWidth = 1;
        return cell;
    }
    if (indexPath.section == 1) {
        FDMMasterPersonalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewMaster" forIndexPath:indexPath];
        FDMMasterModel *model = self.masterArrayOfNew[indexPath.row];
        cell.detailModel = model;
        cell.layer.borderColor = [UIColor grayColor].CGColor;
        cell.layer.borderWidth = 1;
        
        return cell;
    } else {
        FDMMasterPersonalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMMasterPersonalCollectionViewCell" forIndexPath:indexPath];
        FDMMasterKindModel *model = self.dataSourceArr[indexPath.section - 1];
        FDMMasterModel *detailModel = model.listModelArr[indexPath.row];
        cell.detailModel = detailModel;
        cell.layer.borderColor = [UIColor grayColor].CGColor;
        cell.layer.borderWidth = 1;
        
        return cell;
    }
    
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    FDMMasterPersonalCollectionViewCell *cell = (FDMMasterPersonalCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [self.jumpDelegate clickCellToNextControllerWithUserId:cell.userId];
    
}

- (void)dataHandel {
    [APPTools GETWithURL:@"http://mobile.ximalaya.com/m/explore_user_index?device=android&page=1&statEvent=pageview/tab@发现_主播" par:nil success:^(id responseObject) {
        
        self.dataSourceArr = [FDMMasterKindModel baseModelHandelWithArray:responseObject[@"list"]];
        self.masterArrayOfNew = [FDMMasterModel baseModelHandelWithArray:responseObject[@"recommendBozhu"][@"list"]];
        [self.collectionView reloadData];
        
    } failed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
